package org.appframe4gwt.server.persistance;

import xq.gwt.ui.crud.OrderByModel;
import xq.sql.clauses.OrderByClause;
import xq.sql.expression.OrderByExpression;
import xq.sql.expression.OrderByExpression.Order;

public class OrderByClauseBuilder {
		public static OrderByClause buildOrderByClause(OrderByModel[] orderBy){
			if(orderBy == null)
				return null;
			
			OrderByExpression[] expressions = new OrderByExpression[orderBy.length];
			for (int i = 0; i < orderBy.length; i++) {
				expressions[i] = buildOrderByExpression(orderBy[i]);
			}			
			return new OrderByClause(expressions);			
		}
		
		private static OrderByExpression buildOrderByExpression(OrderByModel orderbyModel){
			if (orderbyModel == null)
				return null;
			if(orderbyModel.propertyName == null)
				return null;			
			
			OrderByExpression orderbyExpression = new OrderByExpression(orderbyModel.propertyName);
			Order order = orderbyModel.isAsc ? Order.ASC : Order.DESC; 
			orderbyExpression.setOrder(order);
			return orderbyExpression;
		}
}
